package es.us.lsi.daw.model.security;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

import es.us.lsi.daw.model.security.enums.GroupEnum;
import es.us.lsi.daw.model.util.BaseEntity;


@Entity
@javax.persistence.Table(name = Tables.GROUPS)
public class Group extends BaseEntity {
	public static final String	A_MEMBERS				= "members";
	public static final String	A_AUTHORITIES			= "authorities";
	public static final String	A_GROUP					= "group";
	
	public static final String	TJ_GROUP_AUTHORITIES	= Tables.GROUP_AUTHORITIES;
	
	public static final String	CJ_GROUP_AUTHORITIES_AUTHORITY	= "authority_id";
	public static final String	CJ_GROUP_AUTHORITIES_GROUPS	= "group_id";
	
	
	private static final long serialVersionUID = 1L;
	
	@Enumerated(EnumType.STRING)
	private GroupEnum group;
	
	@ManyToMany(mappedBy = User.A_GROUPS)
	private Set<User> members;
		   
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name = TJ_GROUP_AUTHORITIES, 
			joinColumns=@JoinColumn(name = CJ_GROUP_AUTHORITIES_GROUPS),
			inverseJoinColumns=@JoinColumn(name = CJ_GROUP_AUTHORITIES_AUTHORITY))
	private Set<Authority> authorities;

	public void setGroup(GroupEnum group) {
		this.group = group;
	}

	public GroupEnum getGroup() {
		return group;
	}

	public void setMembers(Set<User> members) {
		this.members = members;
	}

	public Set<User> getMembers() {
		return members;
	}

	public void setAuthorities(Set<Authority> authorities) {
		this.authorities = authorities;
	}

	public Set<Authority> getAuthorities() {
		return authorities;
	}
	
	public String toString(){
		return group.toString();
	}

}
